package cn.csust.net2.manager.server.service.impl;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.transaction.annotation.Transactional;

import cn.csust.net2.manager.server.dao.BaseDAO;
import cn.csust.net2.manager.server.dao.StudentDAO;
import cn.csust.net2.manager.server.mapper.SqlMapper;
import cn.csust.net2.manager.server.remote.BeanUtils;
import cn.csust.net2.manager.server.remote.RemoveServiceBean;
import cn.csust.net2.manager.server.security.AuthCurrentUser;
import cn.csust.net2.manager.shared.exception.BeyondAuthorityException;
import cn.csust.net2.manager.shared.po.PO;
import cn.csust.net2.manager.shared.po.Role;
import cn.csust.net2.manager.shared.po.Student;
import cn.csust.net2.manager.shared.po.Teacher;
import cn.csust.net2.manager.shared.po.User;
import cn.csust.net2.manager.shared.service.BaseService;
import cn.csust.net2.manager.shared.util.Constant;

import com.extjs.gxt.ui.client.data.BaseListLoadConfig;
import com.extjs.gxt.ui.client.data.BasePagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
/**
 * 
 * @author 韩忠金
 *
 * @param <T>
 * 只有在service中或者高层的应用中托管HIBERNATE事物，才可以使HIBERNATE的SESSION与本地线程绑定
 * 可以调用getCurrentSession（）方法，解决了事物因为多线程导致的并发问题
 * 1.一个HTTP请求打开一个新的线程，HTTPSESSION使线程不安全的
 * 2.SWING组建也使线程不安全的，在使用资源的时候要确定同步
 * 3.SESSION的生命周期使现在处于托管模式的，每次请求都会创建然后开启事物，然后关闭事物，最后被关闭
 * 4.LOCALTHREAD变量底层将getCurrentSession（）将SESSION与当前线程绑定，但是LOCALTHREAD并没有与HTTPSESSION
 * 同步
 * 5.避免SESSION的简单数据库操作的提交
 * 
 */
public abstract class BaseServiceImpl implements BaseService,RemoveServiceBean{
	public static final String AUTHORITY_QUERY = "select  * from authority c where c.authorityID in (select authorityID from role_authority where roleID in (select roleID from  user_role where userID = :ID))";
	public static final String USER_SYS = "User";
	@Resource(name = Constant.MAPPER_LOGIN)
	private SqlMapper sqlMapper;
	
	public SqlMapper getSqlMapper() {
		return sqlMapper;
	}
	public void setSqlMapper(SqlMapper sqlMapper) {
		this.sqlMapper = sqlMapper;
	}
	@Resource(name = Constant.DAO_NAME_STUDENT)
	private StudentDAO dao;
	//拿到子类所属的DAO指针
//	public abstract BaseDAO<?> getDAO();
	//reqestHTTP的指针，在远程方法寻找特定BREAN的时候初始化的
	protected HttpServletRequest request;
	//BaseService接口定义接收REQUEST对象的方法
	public void loadRequest(HttpServletRequest request){
		this.request = request;
	}
	/**
	 * 加载当前用户所能查询的范围
	 * 如果是
	 * 1.学生：只能看到本班的（student）
	 * 2.如果是老师;只能看到本院的(teacher)
	 * 3.如果是学校的:能看到全学校的(school)
	 */
	public String getCurrentLoad(){
		String query = "";
		User user = AuthCurrentUser.get();
		List<Role> roles = sqlMapper.loadRole(user.getUserID());
		for(Role r :roles){
			if(r.getRoleName().equals("student")){
				query = " and c.banji.banjiID = "+((Student)user).getBanji().getBanjiID();
			}
		}
		for(Role r :roles){
			if(r.getRoleName().equals("teacher")){
				query = " and c.banji.major.academy.academyID = "+((Teacher)user).getAcademy().getAcademyID();
			}
		}
		return query;
	}
	public String getRole(){
		String query = "";
		User user = AuthCurrentUser.get();
		List<Role> roles = sqlMapper.loadRole(user.getUserID());
		for(Role r :roles){
			if(r.getRoleName().equals("student")){
				query = "student";
			}
		}
		for(Role r :roles){
			if(r.getRoleName().equals("teacher")){
				query = "teacher";
			}
		}
		return query;
	}
	/**
	 * 数据库保存操作
	 */
	@Transactional
	@Override
	public  void save(PO t)throws BeyondAuthorityException {
		
		dao.save(t);
		
	}
	/**
	 * 数据库删除操作
	 */
	@Transactional
	@Override
	public  void delete(PO t) throws BeyondAuthorityException{

		dao.delete(t);
		
	}
	/**
	 * 数据库更新操作
	 */
	@Transactional
	@Override
	public  void update(PO t)throws BeyondAuthorityException {
		
		dao.update(t);
		
	}
	@Transactional
	@Override
	public void save(List<PO> list)throws BeyondAuthorityException{
		for(PO t : list) {
			dao.save(t);
		}
	}
	@Transactional
	@Override
	public void delete(List<PO> list)throws BeyondAuthorityException{
		for(PO t : list) {
			dao.delete(t);
		}
	}
	@Transactional
	@Override
	public void update(List<PO> list)throws BeyondAuthorityException{
		for(PO t : list) {
			dao.update(t);
		}
	}
	/**
	 * 查找全部
	 */
	@Transactional
	@Override
	public <T extends PO> List<T> findAll(String cla)throws BeyondAuthorityException{
		
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.findAll();
	}
	/**
	 * 查找全部
	 */
	@Transactional
	@Override
	public <T extends PO>  BasePagingLoadResult<T> findAll(String cla,PagingLoadConfig config)throws BeyondAuthorityException {
		System.out.println("aaaaasssssdddddd");
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.findAllByPaging(config);
	}
	/**
	 * 查找全部
	 */
	@Transactional
	@Override
	public <T extends PO>   BasePagingLoadResult<T> findAll(String cla,PagingLoadConfig config,String hql)throws BeyondAuthorityException {
		BaseDAO<T> dao = BeanUtils.getBean(cla);
		return dao.findAllByPaging(config,hql);
	}
	
	/**
	 * 根据条件查找全部
	 */
	@Transactional
	@Override
	public <T extends PO> List<T> findAll(String cla, BaseListLoadConfig config)throws BeyondAuthorityException {
	//	// TODO Auto-generated method stub
	//	BaseDAO<T> dao = BeanUtils.getBean(cla);
//		return dao.findAll(config, hql);
		return null;
	}
	
	
	public StudentDAO getDao() {
		return dao;
	}
	public void setDao(StudentDAO dao) {
		this.dao = dao;
	}
	
}
